package com.example.demo.Controller;

import com.example.demo.Mapper.My_assetMapper;
import com.example.demo.Mapper.ProductMapper;
import com.example.demo.entity.MyAssetInfo;
import com.example.demo.entity.ProductInfo;
import com.example.demo.entity.UserInfo;
import com.huawei.shade.org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.sql.Timestamp;

import java.util.List;

@Controller
public class Product {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    My_assetMapper my_assetMapper;

    @RequestMapping("/product")
    public String productInfo(Model model)
    {
        List<ProductInfo> results=productMapper.getProducts();
        model.addAttribute("results",results);
        return "/common/ProductViewer.html";
    }

    @RequestMapping("/addProduct")
    public String productAdd(Model model,@RequestParam(value = "product_id")int product_id,
                            @RequestParam(value = "product_name")String product_name,@RequestParam(value = "discription")String discription,
                            @RequestParam(value = "floor")double floor,@RequestParam(value = "sale_location")String sale_location,
                            @RequestParam(value = "last_time")int last_time)
    {
        int flag=productMapper.addProductInfo(product_id,product_name,discription,floor,sale_location,last_time);
        if(flag == 1)
        {
            List<ProductInfo> results=productMapper.getProducts();
            model.addAttribute("results",results);
            return "/common/ProductViewer.html";
        }
        else {
            return "/error";
        }

    }

    @RequestMapping("/buyProduct")
    public String productBuy(Model model,@RequestParam(value = "product_id")int product_id,
                             @RequestParam(value = "client_id")int client_id,
                             @RequestParam(value = "amount")int amount
                             )
    {
        int flag=productMapper.buyProductInfo(product_id,client_id,amount);
        if(flag == 1)
        {
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            List<MyAssetInfo> results=my_assetMapper.getMyAsset(name);
            model.addAttribute("results",results);
            /*List<ProductInfo> results=productMapper.getProducts();
            model.addAttribute("results",results);*/
            return "/common/MyAssetViewer.html";
        }
        else {
            return "/error";
        }
    }


    @RequestMapping("/sellProduct")
    public String productSell(Model model,@RequestParam(value = "product_id")int product_id,
                             @RequestParam(value = "client_id")int client_id,
                             @RequestParam(value = "sell_amount")int sell_amount
    )
    {
        int flag=productMapper.sellProductInfo(product_id,client_id,sell_amount);
        if(flag == 1)
        {
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            List<MyAssetInfo> results=my_assetMapper.getMyAsset(name);
            model.addAttribute("results",results);
            return "/common/MyAssetViewer.html";
        }
        else {
            return "/error";
        }
    }

    @RequestMapping("/updateProduct")
    public String clientUpdate(Model model,
                               @RequestParam(value = "product_id")int product_id,
                               @RequestParam(value = "product_name")String product_name,
                               @RequestParam(value = "discription")String discription,
                               @RequestParam(value = "floor")double floor,
                               @RequestParam(value = "sale_location")String sale_location,
                               @RequestParam(value = "last_time")int last_time)

    {
        int flag=productMapper.updateProductInfo(product_id,product_name,discription,floor,sale_location,last_time);
        if(flag == 1)
        {
            List<ProductInfo> results=productMapper.getProducts();
            model.addAttribute("results",results);
            return "/common/ProductViewer.html";
        }
        else {
            return "/error";
        }

    }

}
